 var appPath = getAppPath();

$(function(){
	// 检索日期范围
	$("#queryDateStart").datebox({
		formatter:dtFmt,
		parser:dtParser
	});
	$("#queryDateEnd").datebox({
		formatter:dtFmt,
		parser:dtParser
	});
	initJspGrid();
});

function dtFmt(dt){
	var y = dt.getFullYear();
    var m = dt.getMonth()+1;
    var d = dt.getDate();
    return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);//格式化mm-dd
}
function dtParser(s){
    if (!s) return new Date();
    var ss = (s.split('-'));
    var y = parseInt(ss[0],10);
    var m = parseInt(ss[1],10);
    var d = parseInt(ss[2],10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
        return new Date(y,m-1,d);
    } else {
        return new Date();
    }
}

function cancelDown(){
	$('#downDlg').window('close');
}
// 样版列表初始化
function initJspGrid(){
	$('#orderGrid').datagrid({
	    url:appPath + "/itbOrderStats/getYjxOrderCntListinfo.action",
	    height:'full',
	    //remoteSort:false,
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam(),
	    singleSelect:false,
	    // If set to true, clicking a checkbox will always select the row. 
	    // If false, selecting a row will not check the checkbox.
	    selectOnCheck:true,
	    // If true, the checkbox is checked/unchecked when the user clicks on a row. 
	    // If false, the checkbox is only checked/unchecked when the user clicks exactly on the checkbox.
	    checkOnSelect:true,
//	    frozenColumns:[[
//	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
//	    ]],
	    columns:[[	     
	        {field:'months',title:'月 份',width:25,align:'center',sortable:false,
	        	  formatter:function(value, rowData, rowIndex){ 
	        		  if(value){
	        			  return value.substr(0,4)+'-'+value.substr(4,value.length);
	        		  }
	        		  return 'N/A'
	        	  }
	        },
	        {field:'weeks',title:'周 次',width:30,align:'center',sortable:false,
	         	  formatter:function(value, rowData, rowIndex){ 
	        		  if(value){
	        			  return value.substr(0,4)+'-第'+value.substr(4,value.length)+'周';
	        		  }
	        		  return 'N/A'
	        	  }
	        },
	        {field:'days',title:'日 期',width:50,align:'center',sortable:false,
	         	  formatter:function(value, rowData, rowIndex){ 
	        		  if(value){
	        				var date = new Date(value); 
	        			  	var paraYear=date.getFullYear();
	        			  	var paraMonth=date.getMonth();
	        			  	var paraDay=date.getDate();
	        			  	var paraDayOfWeek=date.getDay();
	        			  	if($("#queryOrderDate").datebox('getValue')==2){
		        			    var startWeek=getWeekStartDate(paraYear,paraMonth,paraDay,paraDayOfWeek);
		        			    var endWeek=getWeekEndDate(paraYear,paraMonth,paraDay,paraDayOfWeek);
	        			  		return startWeek +' 至 '+endWeek;
	        			  	}else if($("#queryOrderDate").datebox('getValue')==3){
		        			  	var startMonth=getMonthStartDate(paraYear,paraMonth);
		        			    var endMonth= getMonthEndDate(paraYear,paraMonth);
		        			   return  	startMonth + ' 至 ' +endMonth;
	        			  	}
	        			  	return value;
	        		  }
	        		  return 'N/A'
	        	  }
	        },	      
	        {field:'uCnt',title:'下单用户数',width:30,align:'left',sortable:false,
	        	formatter:function(value, rowData, rowIndex){ 
	        		if(value>=0){
	        			return '<span style="color:green"><font size="3" face="verdana">'+value+'</font>人</span>'
	        		}
	        		return 'N/A'
	        	}
	        },
	        {field:'uPayCnt',title:'支付订单用户数',width:30,align:'left',sortable:false,
		  	   	  formatter:function(value, rowData, rowIndex){ 	
		  	   		  if(value>=0){
		  	   			  return '<span style="color:blue"><font size="3" face="verdana">'+value+'</font>人</span>';
		  	   		  }
		  	   		  return 'N/A';
			  	  }
	        },
	        {field:'order_cnt',title:'下单数',width:40,align:'left',sortable:false,
		  	   	  formatter:function(value, rowData, rowIndex){ 
		  	   		  if(value>=0){
		  	   			  return '<span style="color:green"><font size="3" face="verdana">'+value+'</font>单</span>'
		  	   		  }
		  	   		  return 'N/A'
			  	  }
	        },
	        {field:'pay_cnt',title:'支付订单数',width:30,align:'left',sortable:false,
	        	formatter:function(value, rowData, rowIndex){ 	
	        		if(value>=0){
	        			return '<span style="color:blue"><font size="3" face="verdana">'+value+'</font>单</span>';
	        		}
	        		return 'N/A';
	        	}
	        },
	        {field:'total_fee',title:'支付订单总金额',width:40,align:'left',sortable:false,
	        	formatter:function(value, rowData, rowIndex){
	        		if(value>=0) {
	        			return '<span style="color:red"><font size="3" face="verdana">'+Number(value||'0')/100 +'</font></span> 元';
	        		}
	        		return 'N/A';
	        	}
	        }
	    ]],
	    hideColumn:['user_type'],
	    //view: imgView,// 自定义View
	    pagination:true,
		rownumbers:true,
		toolbar:[],
		rowStyler: function(index,row){
            if ((index % 2)==0){
                return 'background-color:white;color:blue;font-weight:bold;';
            }
        else{
              return 'background-color:white;color:red;font-weight:bold;';
        }
        },
	});
}

//判断日期格式[YYYY-MM-DD]
function chkInputDate(dt){
	 var strDate=dt+'';
    var ss = (strDate.split('-'));
    var y = parseInt(ss[0],10);
    var m = parseInt(ss[1],10);
    var d = parseInt(ss[2],10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
        var chkDt = new Date(y,m-1,d);
		if((chkDt.getFullYear()==y) &&((chkDt.getMonth()+1)==m) &&(chkDt.getDate()==d)) {
        	return dtFmt(chkDt);
        }
    }
    return '';
}

function cancelUpdTmpl(){
	$('#editUserCnt').window('close');
}

function getQueryParam(){
	var queryObject = {};
	var findDtS = $("#queryDateStart").datebox('getValue');//获取开始结束日期
	var findDtE = $("#queryDateEnd").datebox('getValue');
	var findOdt = $("#queryOrderDate").combobox('getValue');
	var findOdrType = $("#queryOrderType").combobox('getValue');
	var findPayPlt = $("#queryPayPlt").combobox('getValue');
	var findPayFrom = $("#queryPayFrom").combobox('getValue');
	var findTestFlg = $("#queryTestFlg").combobox('getValue');
	
	
	findDtS = chkInputDate(findDtS);
	findDtE = chkInputDate(findDtE);
	if(findDtS && findDtS.length>0) queryObject.findDateStart = findDtS +' 00:00:00 ';
	if(findDtE && findDtE.length>0) queryObject.findDateEnd = findDtE+' 23:59:59';
	if(findOdt && findOdt.length>0) queryObject.findOrderDate = findOdt;
	if(findOdrType && findOdrType.length>0) queryObject.findOrderType = findOdrType;
	if(findPayPlt && findPayPlt.length>0) queryObject.findPayPlt = findPayPlt;
	if(findPayFrom && findPayFrom.length>0) queryObject.findPayFrom = findPayFrom;
	if(findTestFlg && findTestFlg.length>0) queryObject.findTestFlg = findTestFlg;// 用户属性 测试用户和非测试用户
	return queryObject;
}

/**
 * 查询数据
 */
function doQuery(){
	$("#orderGrid").datagrid('unselectAll');
	$("#orderGrid").datagrid('load', getQueryParam());
}


//获得某周的开始日期　　 
function getWeekStartDate(paraYear,paraMonth,paraDay,paraDayOfWeek) {　　　 
　 var weekStartDate = new Date(paraYear, paraMonth, paraDay + 1 - paraDayOfWeek);　　　 
　 return formatDate(weekStartDate);　　 
}　　　 

//获得某周的结束日期　　 
function getWeekEndDate(paraYear,paraMonth,paraDay,paraDayOfWeek) {　　　 
　 var weekEndDate = new Date(paraYear, paraMonth, paraDay + (7 - paraDayOfWeek));　　　 
　 return formatDate(weekEndDate);　　 
}　　
//获得某月的开始日期　　 
function getMonthStartDate(paraYear,paraMonth){　　 
　 var monthStartDate = new Date(paraYear, paraMonth, 1);　　　 
　 return formatDate(monthStartDate);　　 
}　　 

//获得某月的结束日期　　 
function getMonthEndDate(paraYear,paraMonth){ 
　 var monthEndDate = new Date(paraYear,paraMonth, getMonthDays(paraYear,paraMonth));　　　 
　 return formatDate(monthEndDate);　　 
}　　 
　 
//格式化
function formatDate(date) {　　　 
	　 var myyear = date.getFullYear();　　 
	　 var mymonth = date.getMonth()+1;　　 
	　 var myweekday = date.getDate();　　　 
	　　　　 
	　 if(mymonth < 10){　　 
	　　　 mymonth = "0" + mymonth;　　 
	　 }　　　 
	　 if(myweekday < 10){　　 
	　　　 myweekday = "0" + myweekday;　　 
	　 }　　 
	　 return (myyear+"-"+mymonth + "-" + myweekday);　　　 
	}　

//获得某月的天数　　 
function getMonthDays(paraYear,paraMonth){　　 
　 var monthStartDate = new Date(paraYear, paraMonth, 1);　　　 
　 var monthEndDate = new Date(paraYear, paraMonth + 1, 1);　　　 
　 var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);　　　 
　 return days;
}　



